package com.manage.controller;

import com.beans.Customer;
import com.beans.HouseInspectNote;
import com.beans.page.PageList;
import com.commons.util.StringUtil;
import com.manage.util.UserUtils;
import com.services.CustomerService;
import com.services.HouseInspectNoteInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/20
 */
@Controller
@RequestMapping("inspectNote")
public class InspectNotoController extends BaseController {

    @Autowired
    private HouseInspectNoteInfoService houseInspectNoteInfoService;
    @Autowired
    private CustomerService customerService;

    /**
     * 查询列表
     *
     * @param houseInspectNote
     * @return
     */
    @RequiresPermissions("inspectNote:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(HouseInspectNote houseInspectNote, Long relationType, Long relationId,HttpServletRequest request) {

        houseInspectNote.setRelationId(relationId);
        houseInspectNote.setRelationType(relationType);

        Customer customer = new Customer();

        customer.setCreateBy(UserUtils.getUser().getUserId());
        List customers = customerService.queryList(customer);

        request.setAttribute("houseInspectNote", houseInspectNote);
        request.setAttribute("customers", customers);
        return "inspectNote/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param houseInspectNote
     * @return
     */
    @RequiresPermissions("inspectNote:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, HouseInspectNote houseInspectNote) {
        return houseInspectNoteInfoService.queryPage(pageList, houseInspectNote);
    }

    @RequiresPermissions("inspectNote:edit")
    @RequestMapping("save")
    @ResponseBody
    public boolean add(Long relationId, Long customerId, Long relationType, String inspectNoteInfo, String inspectNoteDate){
        boolean returnValue = false;

        HouseInspectNote houseInspectNote = new HouseInspectNote();

        houseInspectNote.setCreateBy(UserUtils.getUser().getUserId());
        houseInspectNote.setUpdateBy(UserUtils.getUser().getUserId());

        houseInspectNote.setUpdateDate(StringUtil.StirngToTimestamp(inspectNoteDate + " 00:00:00"));

        houseInspectNote.setRelationId(relationId);
        houseInspectNote.setRelationType(relationType);//1:房源, 2:新楼盘
        houseInspectNote.setCustomerId(customerId);
        houseInspectNote.setText(inspectNoteInfo);

        int rsout = houseInspectNoteInfoService.add(houseInspectNote);

        if(rsout > 0){
            returnValue = true;
        }

        return returnValue;
    }
}
